package com.sskj.fabi.util;

import android.content.Context;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.MD5Util;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.fabi.R;
import com.sskj.fabi.bean.FaBiDialogBean;

import org.w3c.dom.Text;

/**
 * Dialog 展示
 */
public class TipFabiUtil {
    public static AlertDialog customTip1(Context context, boolean isBuy, FaBiDialogBean faBiDialogBean, OnTipSure onTipSure) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.fabi_dialog_show, null);
        ((TextView) view.findViewById(R.id.dialog_valuation)).setText(isBuy ? "购买单价" : "出售单价");
        ((TextView) view.findViewById(R.id.dialog_valuation_end)).setText(NumberUtil.keep2(faBiDialogBean.getPayPrice()) + " CNY");
        ((TextView) view.findViewById(R.id.dialog_num)).setText(isBuy ? "购买数量" : "出售数量");
        ((TextView) view.findViewById(R.id.dialog_num_end)).setText(NumberUtil.keep4(faBiDialogBean.getPayNum()) + " BDD");
        ((TextView) view.findViewById(R.id.isBuy_num)).setText(isBuy ? "购买金额" : "出售金额");
        ((TextView) view.findViewById(R.id.dialog_money)).setText(NumberUtil.keep2(faBiDialogBean.getMoney()) + " CNY");
        builder.setView(view);
        builder.setCancelable(true);
        AlertDialog tipDialog = builder.create();
        ClickUtil.click(view.findViewById(R.id.dialog_cancel), () -> {
            tipDialog.dismiss();
        });
        ClickUtil.click(view.findViewById(R.id.dialog_confirm), () -> {
            String editPwd = ((EditText) view.findViewById(R.id.dialog_pay_pwd)).getText().toString().trim();
            if (CheckUtil.isPayPwd(editPwd)) {
                tipDialog.dismiss();
                onTipSure.onSure(MD5Util.getMd5Value(editPwd));
            } else {
                ToastUtil.showShort("支付密码输入格式不正确");
            }
        });
        return tipDialog;
    }

    public static AlertDialog customTip2(Context context, FaBiDialogBean faBiDialogBean, OnTipSure onTipSure) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.fabi_dialog_show2, null);
        ((TextView) view.findViewById(R.id.fabi_dialog_type)).setText(App.INSTANCE.getString(R.string.fabiTipFabiUtil0).equals(faBiDialogBean.getTitleType()) ? App.INSTANCE.getString(R.string.fabiTipFabiUtil21) : App.INSTANCE.getString(R.string.fabiTipFabiUtil22));

        ((TextView) view.findViewById(R.id.dialog_valuation)).setText(App.INSTANCE.getString(R.string.fabiTipFabiUtil0).equals(faBiDialogBean.getTitleType()) ? App.INSTANCE.getString(R.string.fabiTipFabiUtil11) : App.INSTANCE.getString(R.string.fabiTipFabiUtil12));
        ((TextView) view.findViewById(R.id.dialog_valuation_end)).setText(faBiDialogBean.getPayPrice());
        ((TextView) view.findViewById(R.id.dialog_num)).setText(App.INSTANCE.getString(R.string.fabiTipFabiUtil0).equals(faBiDialogBean.getTitleType()) ? App.INSTANCE.getString(R.string.fabiTipFabiUtil14) : App.INSTANCE.getString(R.string.fabiTipFabiUtil15));
        ((TextView) view.findViewById(R.id.dialog_num_end)).setText(faBiDialogBean.getPayNum());
        ((TextView) view.findViewById(R.id.dialog_money)).setText(faBiDialogBean.getMoney());
        ((TextView) view.findViewById(R.id.dialog2_minnum)).setText(faBiDialogBean.getMinNum());
        ((TextView) view.findViewById(R.id.dialog2_maxnum)).setText(faBiDialogBean.getMaxNum());
        String[] payArray = faBiDialogBean.getPayClientType().split(",");
        StringBuffer payRecord = new StringBuffer();
        for (int i = 0; i < payArray.length; i++) {
            if ("2".equals(payArray[i])) {
                ((TextView) view.findViewById(R.id.dialog_pay_alipay)).setVisibility(View.VISIBLE);
            }
            if ("1".equals(payArray[i])) {
                ((TextView) view.findViewById(R.id.dialog_pay_wx)).setVisibility(View.VISIBLE);
            }
            if ("3".equals(payArray[i])) {
                ((TextView) view.findViewById(R.id.dialog_pay_bank)).setVisibility(View.VISIBLE);
            }
        }
        builder.setView(view);
        builder.setCancelable(true);
        AlertDialog tipDialog = builder.create();
        ClickUtil.click(view.findViewById(R.id.dialog_cancel), () -> {
            tipDialog.dismiss();
        });
        ClickUtil.click(view.findViewById(R.id.dialog_confirm), () -> {
            tipDialog.dismiss();
            onTipSure.onSure(MD5Util.getMd5Value(((TextView) view.findViewById(R.id.dialog_pay_pwd)).getText().toString().trim()));
        });
        return tipDialog;
    }

    public static MaterialDialog showAppeal(Context context, OnTipAppealSure onTipAppealSure) {

        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.fabi_dialog_appeal, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customeView = dialog.getCustomView();


        Spinner spinner = customeView.findViewById(R.id.spinner);
        String[] stringArray = context.getResources().getStringArray(R.array.fabi_appeal);
        spinner.setAdapter(new ArrayAdapter<String>(context, R.layout.fabi_spinner_text, stringArray));
        EditText etReason = customeView.findViewById(R.id.etReason);

        ClickUtil.click(customeView.findViewById(R.id.ivBack), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(customeView.findViewById(R.id.tvCancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(customeView.findViewById(R.id.tvSure), () -> {
            dialog.dismiss();
            onTipAppealSure.onSure(spinner.getSelectedItemPosition(), etReason.getText().toString());
        });
        return dialog;
    }

    public static BottomSheetDialog getPay(Context context, OnPwdGet onPwdGet) {

        BottomSheetDialog codeDialog = new BottomSheetDialog(context);
        View dialogView = LayoutInflater.from(context)
                .inflate(R.layout.fabi_bottom_sheet_check_code, null);
        EditText mPayPwdEditText = dialogView.findViewById(R.id.etCode);
        ClickUtil.click(dialogView.findViewById(R.id.tvCancel), codeDialog::dismiss);
        codeDialog.setContentView(dialogView);
        ClickUtil.click(dialogView.findViewById(R.id.btSubmit), () -> { //提交修改
            String pwd = mPayPwdEditText.getText().toString().trim();
            if (TextUtils.isEmpty(pwd)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabiTipFabiUtil33));
                return;
            }
            if (!CheckUtil.isPayPwd(pwd)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabiTipFabiUtil34));
                return;
            }
            onPwdGet.onSure(pwd);

        });
        return codeDialog;

    }

    public interface OnPwdGet {
        void onSure(String pwd);
    }


    public interface OnTipSure {
        void onSure(String payPwd);
    }

    public interface OnTipAppealSure {
        void onSure(int pos, String reason);
    }
}
